package game.handler.implement;

import game.enums.KeyID;

import java.awt.event.KeyEvent;
import java.util.HashMap;

public class gameKeyboardHandler{
	
	private HashMap<KeyID,gameKeyState> keyPressMap;
	private HashMap<Integer,KeyID> keymap;
	

	public gameKeyboardHandler(HashMap<KeyID,gameKeyState> keyPressMap,HashMap<Integer,KeyID> keymap){
		this.keyPressMap=keyPressMap;
		this.keymap=keymap; 
	}
	
	public void handleKeyEvent(KeyEvent e){
		int code = e.getKeyCode();
		if (keymap.containsKey(code))
			keyPressMap.get(keymap.get(code)).keyDown=e.getID()==KeyEvent.KEY_PRESSED;
	}
	
	public static HashMap<KeyID, gameKeyState>  createKeyPressMap(){
		HashMap<KeyID, gameKeyState> keyPressMap=new HashMap<>();
		keyPressMap.put(KeyID.UP, new gameKeyState());
		keyPressMap.put(KeyID.DOWN, new gameKeyState());
		keyPressMap.put(KeyID.LEFT, new gameKeyState());
		keyPressMap.put(KeyID.RIGHT, new gameKeyState());
		keyPressMap.put(KeyID.USE, new gameKeyState());
		keyPressMap.put(KeyID.ENTER, new gameKeyState());
		keyPressMap.put(KeyID.ESC, new gameKeyState());
		keyPressMap.put(KeyID.LISTUP, new gameKeyState());
		keyPressMap.put(KeyID.LISTDOWN, new gameKeyState());
		return keyPressMap;
	}
	
	public static HashMap<Integer,KeyID> createDefaultKeyMap(){
		HashMap<Integer,KeyID> keymap=new HashMap<>();
		keymap.put(KeyEvent.VK_E, KeyID.USE);
		keymap.put(KeyEvent.VK_W, KeyID.UP);
		keymap.put(KeyEvent.VK_S, KeyID.DOWN);
		keymap.put(KeyEvent.VK_A, KeyID.LEFT);
		keymap.put(KeyEvent.VK_D, KeyID.RIGHT);
		keymap.put(KeyEvent.VK_E, KeyID.LISTUP);
		keymap.put(KeyEvent.VK_Q, KeyID.LISTDOWN);
		keymap.put(KeyEvent.VK_ENTER, KeyID.ENTER);
		keymap.put(KeyEvent.VK_ESCAPE, KeyID.ESC);
		return keymap;
	}
	
 }
